package com.hdoit.ares.web.cust.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.model.AdviceFeedback;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.IAdviceFeedbackService;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.selfcommon.enums.cust.AdviceStatus;
import com.hdoit.ares.selfcommon.utils.EmojiUtil;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;
import com.wordnik.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/api/v1/AdviceFeedbackAres/",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value="/api/v1/AdviceFeedbackAres/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class AdviceFeedbackApi extends BaseApi{

	private ICustomerService customerService;
	private IAdviceFeedbackService adviceFeedbackService;

	
	public ICustomerService getCustomerService() {
		return customerService;
	}
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	
	public IAdviceFeedbackService getAdviceFeedbackService() {
		return adviceFeedbackService;
	}
	@Resource
	public void setAdviceFeedbackService(
			IAdviceFeedbackService adviceFeedbackService) {
		this.adviceFeedbackService = adviceFeedbackService;
	}
	
	
	private final String submitAdviceApi = "submitAdvice";
	private final String feedbackListApi = "feedbackList";
	
	@ResponseBody
	@RequestMapping(value = submitAdviceApi)
	@ApiOperation(notes= submitAdviceApi,httpMethod = "POST" ,value = "提交反馈")
	public String submitAdvice(
			@ApiParam("内容") @RequestParam(value="advice",required=true)String advice,
			@ApiParam("联系方式") @RequestParam(value="contact",required=true)String contact
	){	
		ResponseModel rm = new ResponseModel();
		
		if(StringUtils.isEmpty(advice)||StringUtils.isEmpty(contact)){
			rm.setStatus(ResponseStatus.dataError);
			rm.setMessage("内容或联系方式为空");
			return rm.toJson();
		}
		
		Customer customer=customerService.queryModel(getLoginId()); 
		AdviceFeedback adviceFeedback = new AdviceFeedback();
		
		adviceFeedback.setAdvice(EmojiUtil.emojiConverterToAlias(advice));
		adviceFeedback.setContact(contact);
		adviceFeedback.setId(CommonUtils.getKeyId());
		adviceFeedback.setAdviceId(customer.getId());
		adviceFeedback.setCreateDate(DateUtils.getCurrent());
		adviceFeedback.setAdvicestate(AdviceStatus.Handling);
		adviceFeedback.setDeleteMark(YesOrNo.no);
		adviceFeedback.setModelStatus(ModelStatus.add);
		boolean result = adviceFeedbackService.applyChange(adviceFeedback);
		rm.put("result", result);
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = feedbackListApi)
	@ApiOperation(notes= feedbackListApi,httpMethod = "POST" ,value = "意见反馈列表")
	public String feedbackList(){
		
		ResponseModel rm = new ResponseModel();

		Customer customer=customerService.queryModel(getLoginId()); 
		List<AdviceFeedback> adviceFeedbacks = adviceFeedbackService.queryListWechat(customer.getId());
		
		for (AdviceFeedback adviceFeedback : adviceFeedbacks) {
			adviceFeedback.setAdvice(EmojiUtil.emojiConverterUnicodeStr(adviceFeedback.getAdvice()));
		}
		
		if(adviceFeedbacks.size()>=0){
			rm.setStatus(ResponseStatus.success);
			List<String> params = new ArrayList<String>();
			Field[] fields = AdviceFeedback.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			rm.putList("adviceFeedbacks", adviceFeedbacks, params);
		}
		return rm.toJson();
	}
}
